package com.qjdchina.sku.front.web.controller;

import com.qjdchina.common.web.util.RestBody;
import com.qjdchina.commons.model.PageModel;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.CaseStoreInfoManager;
import com.qjdchina.sku.model.vo.CaseStoreInfoVO;
import com.qjdchina.sku.service.enums.SkuResultCode;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Luoyan on 2017/6/11.
 */
@Controller
@RequestMapping(value = "case")
public class CaseStoreInfoController {
    public static final Logger LOGGER = LoggerFactory.getLogger(CaseStoreInfoController.class);

    @Autowired
    private CaseStoreInfoManager caseStoreInfoManager;

    /**
     * 部品案例库列表
     *
     * @param strPage
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping(value = "query", method = RequestMethod.POST)
    @ResponseBody
    public RestBody<PageModel<CaseStoreInfoVO>> query(Integer productId, @RequestParam(value = "pageNo", required = false)
            String strPage, @RequestParam(value = "pageSize", required = false) Integer pageSize, HttpServletRequest request) {

        PageModel<CaseStoreInfoVO> pageModel = new PageModel<CaseStoreInfoVO>();
        int pageNo = 1;
        if (StringUtils.isNotEmpty(strPage) && StringUtils.isNumeric(strPage)) {
            pageNo = Integer.parseInt(strPage);
        }
        pageModel.setPageNo(pageNo);
        if (pageSize == null) {
            pageSize = 15;
        }
        pageModel.setPageSize(pageSize);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productId", productId);
        Result<PageModel<CaseStoreInfoVO>> result = caseStoreInfoManager.queryPagedCaseStoreInfoList(params, pageModel);
        if (!result.isSuccess()) {
            return new RestBody<PageModel<CaseStoreInfoVO>>(result.getCode(), result.getMessage());
        }
        PageModel<CaseStoreInfoVO> pageCaseStoreInfo = result.getDataModel();

        return new RestBody<PageModel<CaseStoreInfoVO>>(SkuResultCode.SUCCESS, pageCaseStoreInfo);
    }

    /**
     * 部品案例库详情
     *
     * @param caseStoreInfoId
     * @return
     */
    @RequestMapping(value = "caseStoreInfoDetail", method = RequestMethod.GET)
    @ResponseBody
    public RestBody<CaseStoreInfoVO> caseStoreInfoDetail(Integer caseStoreInfoId) {
        Result<CaseStoreInfoVO> result = caseStoreInfoManager.queryCaseStoreInfoVODetail(caseStoreInfoId);
        if (!result.isSuccess()) {
            return new RestBody<CaseStoreInfoVO>(result.getCode(), result.getMessage());
        }
        return new RestBody<CaseStoreInfoVO>(SkuResultCode.SUCCESS, result.getDataModel());
    }

    /**
     * 部品案例库新增
     *
     * @param caseStoreInfoVO
     * @return
     */
    @RequestMapping(value = "createCaseStoreInfo", method = RequestMethod.POST)
    @ResponseBody
    public RestBody<Integer> createCaseStoreInfo(CaseStoreInfoVO caseStoreInfoVO) {
        Result<Integer> result = caseStoreInfoManager.createCaseStoreInfo(caseStoreInfoVO);
        if (!result.isSuccess()) {
            return new RestBody<Integer>(result.getCode(), result.getMessage());
        }
        return new RestBody<Integer>(SkuResultCode.SUCCESS, result.getDataModel());

    }
}
